import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {AxdrSequenceOf} from "../asn1.axdr/AxdrSequenceOf";
import {A_ResultNormal} from "./A_ResultNormal";
import {A_ResultRecord} from "./A_ResultRecord";
import {Buffer} from "buffer";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";



enum Choice {
    _ERR_NONE_SELECTED = -1,
    RESULT_NORMAL = 1,
    RESULT_RECORD = 2,
}

export class SubSeqOfResultNormal extends AxdrSequenceOf<A_ResultNormal>{

    createListElement(): A_ResultNormal {
        return new A_ResultNormal();
    }

    set_length(length: number) {
        super.set_length(length);
    }

    constructor() {
        super();
    }

}

export class SubSeqOfResultRecord extends AxdrSequenceOf<A_ResultRecord>{

    createListElement(): A_ResultRecord {
        return new A_ResultRecord();
    }

    set_length(length: number) {
        super.set_length(length);
    }

    constructor() {
        super();
    }

}

export class FollowReport implements AxdrType{

    dataCode : Buffer | null = null;
    choice : Choice = Choice._ERR_NONE_SELECTED;
    resultNormal : SubSeqOfResultNormal | null = null;
    resultRecord : SubSeqOfResultRecord | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    setResultNormal(resultNormal : SubSeqOfResultNormal){
        this.resetChoices();
        this.choice = Choice.RESULT_NORMAL;
        this.resultNormal = resultNormal;
    }

    setResultRecord(resultRecord : SubSeqOfResultRecord){
        this.resetChoices();
        this.choice = Choice.RESULT_RECORD;
        this.resultRecord = resultRecord;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.RESULT_NORMAL) {
            this.resultNormal = new SubSeqOfResultNormal();
            codeLength += this.resultNormal.decode(input);
            return codeLength;
        }else if (this.choice == Choice.RESULT_RECORD) {
            this.resultRecord = new SubSeqOfResultRecord();
            codeLength += this.resultRecord.decode(input);
            return codeLength;
        }

        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;

        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.RESULT_RECORD) {
            if(this.resultRecord != null){
                codeLength += this.resultRecord.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(2);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("resultRecord is null");
            }

        }else if (this.choice == Choice.RESULT_NORMAL) {
            if(this.resultNormal != null){
                codeLength += this.resultNormal.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(1)
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("resultNormal is null");
            }
        }

        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.resultNormal = null;
        this.resultRecord = null;
    }

    toString(): string {
        if (this.choice == Choice.RESULT_RECORD) {
            if (this.resultRecord != null) {
                return "choice: {resultRecord: " + this.resultRecord + "}";
            } else {
                return "choice is resultRecord but resultRecord is null";
            }
        }else if(this.choice == Choice.RESULT_NORMAL){
            if(this.resultNormal != null){
                return "choice: {resultNormal: " + this.resultNormal + "}";
            }else{
                return "choice is resultNormal but resultNormal is null";
            }
        } else {
            return "unknown";
        }
    }



}
